package com.tedu.egweibo2.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.egweibo2.base.response.JsonResult;
import com.tedu.egweibo2.base.response.StatusCode;
import com.tedu.egweibo2.mapper.CommentMapper;
import com.tedu.egweibo2.pojo.dto.CommentAddParam;
import com.tedu.egweibo2.pojo.dto.CommentUpdateParam;
import com.tedu.egweibo2.pojo.entity.Comment;
import com.tedu.egweibo2.pojo.vo.CommentVO;
import com.tedu.egweibo2.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论模块")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation(value = "发表评论")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, @ApiIgnore HttpSession session) {
        log.debug("" + commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        comment.setUserId(userVO.getId());
        comment.setIsDelete(0);

        log.debug("" + comment);
        int count = commentMapper.insertComment(comment);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation(value = "获取评论列表")
    @ApiOperationSupport(order = 20)
    @ApiImplicitParam(name = "weiboId", value = "微博编号", required = true)
    public JsonResult selectByWeiboId(Long weiboId) {
        log.debug("weiboId: " + weiboId);
        List<CommentVO> commentVOList = commentMapper.selectCommentListByWeiboId(weiboId);
        return commentVOList != null ? JsonResult.ok(commentVOList) : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改评论")
    @ApiOperationSupport(order = 30)
    public JsonResult update(@RequestBody CommentUpdateParam commentUpdateParam, @ApiIgnore HttpSession session) {
        log.debug("" + commentUpdateParam);
        UserVO userVO = (UserVO) session.getAttribute("user");

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentUpdateParam, comment);
        comment.setUpdateTime(new Date());

        log.debug("" + comment);
        int count = commentMapper.updateComment(comment);
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 删除评论，伪删除
     *
     * @param isDelete
     * @param id
     * @param session
     * @return
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除评论")
    @ApiOperationSupport(order = 40)
    @ApiImplicitParams(
            value = {
                    @ApiImplicitParam(name = "isDelete", value = "是否确认删除", example = "输入\"确认删除\"，即可完成删除"),
                    @ApiImplicitParam(name = "id", value = "评论编号", required = true)
            }
    )
    public JsonResult delete(String isDelete, Long id, @ApiIgnore HttpSession session) {
        log.debug(isDelete + ",id: " + id);
        UserVO userVO = (UserVO) session.getAttribute("user");

        int count = 0;
        Long userId = userVO.getId();
        if ("确认删除".equals(isDelete.trim())) {
            count = commentMapper.deleteCommentById(id, userId);
        }
        return count > 0 ? JsonResult.ok() : new JsonResult(StatusCode.OPERATION_FAILED);
    }

}
